package com.company.psm.serviceimpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.company.psm.entity.Gradient;
import com.company.psm.entity.Store;
import com.company.psm.genericdao.CustomGenericDAO;
import com.company.psm.service.GradientManagement;
@Service(value="gradientManagement")
@Transactional(readOnly = true, propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
public class GradientManagementImpl extends CustomGenericDAO<Gradient> implements GradientManagement {

	public List<Gradient> getAll() {
		// TODO Auto-generated method stub
		return _all(Gradient.class);
	}

	public Gradient find(Long arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public Gradient[] find(Long... arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public Gradient getReference(Long arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public Gradient[] getReferences(Long... arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean removeById(Long arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	public void removeByIds(Long... arg0) {
		// TODO Auto-generated method stub
		
	}
	@Transactional(readOnly = false)
	public void createNewGradient(String gradientName_, long storeId_) {
		// TODO Auto-generated method stub
		Gradient gradient = new Gradient();
		gradient.setName(gradientName_);
		Store store = _load(Store.class, storeId_);
		gradient.setStore(store);
		_save(gradient);
	}
	@Transactional(readOnly = true)
	public List<Gradient> getListOfGradientsByStoreId(long storeId_) {
		// TODO Auto-generated method stub
		List<Gradient> result = new ArrayList<Gradient>();
		List<Gradient> allGradient = this.getAll();
		for (Gradient gradient : allGradient) {
			if(gradient.getStore().getId() == storeId_){
				result.add(gradient);
			}
		}
		return result;
	}

	public Gradient getGradientWithNameAndStoreId(String gradientName_,
			long storeId_) {
		// TODO Auto-generated method stub
		Gradient result = new Gradient();
		List<Gradient> gradientsList = this.getAll();
		for (Gradient gradient : gradientsList) {
			if(gradient.getStore().getId() == storeId_ || gradient.getStore().getId() == 3){
				if(gradient.getName().equalsIgnoreCase(gradientName_)){
					result = gradient;
				}
			}
		}
		return result;
	}
	@Transactional(readOnly = false)
	public void updateGradient(String id, String name, long storeId) {
		Gradient gradient = _get(Gradient.class, Long.parseLong(id));
		gradient.setName(name);
		Store store = _get(Store.class, storeId);
		gradient.setName(name);
		gradient.setStore(store);
		_update(gradient);
	}

	
}
